/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormularioEmpresa.java
 *
 * Created on 8/01/2011, 04:39:44 PM
 */
package ventanas.pasosiniciales;

import beans.Empresa;
import beans.Ciudad;
import db.Model;
import java.util.HashMap;
import java.util.List;
import javax.swing.JOptionPane;
import utilidades.Validaciones;

/**
 *
 * @author Usuario
 */
public class FormularioEmpresa extends javax.swing.JPanel {

    /** Creates new form FormularioEmpresa */
    public FormularioEmpresa() {
        initComponents();
        iniciar();
    }

    public void iniciar() {
        cbxciudad.removeAllItems();
        try {

            List<Ciudad> listaCiudades = (List<Ciudad>) Model.getInstance().obtenerListado("obtenerCiudades", "%");

            for (Ciudad obj : listaCiudades) {
                cbxciudad.addItem(obj);

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        txtnombre.requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

    

    public Empresa getEmpresa() {

        Empresa emp = new Empresa();

        emp.setNombre(txtnombre.getText());
        emp.setNit(Integer.parseInt(txtnit.getText()));
        emp.setCiudad((Ciudad) cbxciudad.getSelectedItem());
        emp.setDireccion(txtdireccion.getText());
        emp.setEmail(txtemail.getText());
        emp.setPropietario(txtpropietario.getText());
        emp.setTelefono(txttelefono.getText());
        emp.setLicencia(txtlicencia.getText());
        emp.setEstado(interfaces.Constantes.ESTADO_EMPRESA_ACTIVO+"");


        return emp;
    }

    public boolean validar() {
        
        if (txtnombre.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, "Ingrese el NOMBRE de la EMPRESA", "Se ha producido un inconveniente", JOptionPane.ERROR_MESSAGE);
            txtnombre.requestFocus();
            return false;
        } else if (txtnit.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, "Ingrese el NIT de la EMPRESA", "Se ha producido un inconveniente", JOptionPane.ERROR_MESSAGE);
            txtnit.requestFocus();
            return false;
        } else if (txtnit.getText().length()<8) {
            JOptionPane.showMessageDialog(null, "El NIT de la EMPRESA debe tener almenos 8 digitos", "Se ha producido un inconveniente", JOptionPane.ERROR_MESSAGE);
            txtnit.requestFocus();
            return false;
        }else if (txtdireccion.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, "Ingrese la DIRECCIÓN de la EMPRESA", "Se ha producido un inconveniente", JOptionPane.ERROR_MESSAGE);
            txtdireccion.requestFocus();
            return false;
        } else if (txtemail.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, "Ingrese el E-MAL de la EMPRESA", "Se ha producido un inconveniente", JOptionPane.ERROR_MESSAGE);
            txtemail.requestFocus();
            return false;
        }  else if (!Validaciones.esEmail(txtemail.getText())) {
            JOptionPane.showMessageDialog(null, "Digite un E-MAIL válido por favor");
            txtemail.requestFocus();
            return false;
        }else if (txtpropietario.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, "Ingrese el PROPIETARIO de la EMPRESA", "Se ha producido un inconveniente", JOptionPane.ERROR_MESSAGE);
            txtpropietario.requestFocus();
            return false;
        } else if (txttelefono.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, "Ingrese el TELÉFONO de la EMPRESA", "Se ha producido un inconveniente", JOptionPane.ERROR_MESSAGE);
            txttelefono.requestFocus();
            return false;
        }else if (txttelefono.getText().length()<7) {
            JOptionPane.showMessageDialog(null, "El TELÉFONO de la EMPRESA debe tener al menos 7 digitos", "Se ha producido un inconveniente", JOptionPane.ERROR_MESSAGE);
            txttelefono.requestFocus();
            return false;
        } else if (txtlicencia.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, "Ingrese la LICENCIA asignada a la EMPRESA", "Se ha producido un inconveniente", JOptionPane.ERROR_MESSAGE);
            txtlicencia.requestFocus();
            return true;
        }
        return true;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cbxciudad = new javax.swing.JComboBox();
        txtnombre = new javax.swing.JTextField();
        txtemail = new javax.swing.JTextField();
        txttelefono = new javax.swing.JTextField();
        txtpropietario = new javax.swing.JTextField();
        txtdireccion = new javax.swing.JTextField();
        txtlicencia = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtnit = new javax.swing.JFormattedTextField();

        jPanel1.setBackground(new java.awt.Color(86, 177, 185));

        cbxciudad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cartagena", "Bogota", "Medellin" }));

        txtnombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtnombreKeyTyped(evt);
            }
        });

        txtemail.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtemailKeyTyped(evt);
            }
        });

        txttelefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txttelefonoKeyTyped(evt);
            }
        });

        txtpropietario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtpropietarioKeyTyped(evt);
            }
        });

        txtdireccion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtdireccionKeyReleased(evt);
            }
        });

        txtlicencia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtlicenciaKeyTyped(evt);
            }
        });

        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Nombre");

        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("NIT");

        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("E-mail");

        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Telefono");

        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Propietario");

        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("Ciudad");

        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setText("Dirección");

        jLabel8.setForeground(new java.awt.Color(255, 255, 255));
        jLabel8.setText("Licencia");

        txtnit.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        txtnit.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        txtnit.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtnitKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel8)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtemail, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                    .addComponent(cbxciudad, 0, 238, Short.MAX_VALUE)
                    .addComponent(txtpropietario, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                    .addComponent(txtlicencia, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                    .addComponent(txtdireccion, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                    .addComponent(txtnombre, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                    .addComponent(txttelefono, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                    .addComponent(txtnit, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtnombre, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(4, 4, 4)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtnit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtemail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(7, 7, 7)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txttelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtpropietario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbxciudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel6)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtdireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtlicencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addContainerGap(45, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txttelefonoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txttelefonoKeyTyped
        // TODO add your handling code here:
          if(!Character.isDigit(evt.getKeyChar())||txttelefono.getText().length()>=12)
            evt.consume();
    }//GEN-LAST:event_txttelefonoKeyTyped

    private void txtnitKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtnitKeyTyped
        // TODO add your handling code here:
        if(!Character.isDigit(evt.getKeyChar()) || txtnit.getText().length() >= 17)
            evt.consume();
    }//GEN-LAST:event_txtnitKeyTyped

    private void txtnombreKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtnombreKeyTyped
        // TODO add your handling code here:
        if ( txtnombre.getText().length() >= 45) {
            evt.consume();
        }
    }//GEN-LAST:event_txtnombreKeyTyped

    private void txtemailKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtemailKeyTyped
        // TODO add your handling code here:
                if ( txtemail.getText().length() >= 45) {
            evt.consume();
        }
    }//GEN-LAST:event_txtemailKeyTyped

    private void txtpropietarioKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtpropietarioKeyTyped
        // TODO add your handling code here:
         if ( txtpropietario.getText().length() >= 45) {
            evt.consume();
        }
    }//GEN-LAST:event_txtpropietarioKeyTyped

    private void txtdireccionKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtdireccionKeyReleased
        // TODO add your handling code here:
       if ( txtdireccion.getText().length() >= 45) {
            evt.consume();
        }
    }//GEN-LAST:event_txtdireccionKeyReleased

    private void txtlicenciaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtlicenciaKeyTyped
        // TODO add your handling code here:
       if ( txtlicencia.getText().length() >= 45) {
            evt.consume();
        }
    }//GEN-LAST:event_txtlicenciaKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbxciudad;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtdireccion;
    private javax.swing.JTextField txtemail;
    private javax.swing.JTextField txtlicencia;
    private javax.swing.JFormattedTextField txtnit;
    private javax.swing.JTextField txtnombre;
    private javax.swing.JTextField txtpropietario;
    private javax.swing.JTextField txttelefono;
    // End of variables declaration//GEN-END:variables
}
